package flatlandAgent;

import java.awt.GridBagConstraints;
import java.util.Arrays;

import javax.swing.ButtonGroup;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

import evolutionaryLoop.EvolutionaryParameters;
import evolutionaryProblems.Problem;
import gui.SettingPanel;

public class FlatlandSettingPanel extends SettingPanel{

	private static final long serialVersionUID = 1L;
	private JTextField foodDist;
	private JTextField poisonDist;
	private JLabel hiddenLayers;
	private JTextField layers;
	private JRadioButton staticButton;
	private JRadioButton dynamicButton;
	private ButtonGroup buttonGroup; 

	
	public FlatlandSettingPanel(Problem problem, EvolutionaryParameters parameters) {
		super(problem, parameters);
	}

	@Override
	protected void initComponents(){
		super.initComponents(); 

		JLabel foodLabel = new JLabel("Food");
		addComponent(foodLabel, 0, 5);
		foodDist = new JTextField();
		foodDist.setColumns(10);
		addComponent(foodDist, 1, 5); 
		
		JLabel poisonLabel = new JLabel("Poison");
		addComponent(poisonLabel, 2, 5); 
		poisonDist = new JTextField();
		poisonDist.setColumns(10);
		addComponent(poisonDist, 3, 5); 
		
		hiddenLayers = new JLabel("Hidden layers");
		addComponent(hiddenLayers, 0, 6); 
		addHiddenLayerField();
		
		
		buttonGroup = new ButtonGroup(); 
		staticButton = new JRadioButton("Static");
		buttonGroup.add(staticButton);
		addComponent(staticButton, 0, 7); 
		
		dynamicButton = new JRadioButton("Dynamic");
		buttonGroup.add(dynamicButton);
		addComponent(dynamicButton, 2, 7); 
	}

	private void addHiddenLayerField() {
		layers = new JTextField();
		layers.setColumns(10);
		GridBagConstraints constraint = new GridBagConstraints(); 
		constraint.gridx = 1; 
		constraint.gridy = 6; 
		constraint.gridwidth = 3; 
		constraint.anchor = GridBagConstraints.WEST; 
		this.add(layers, constraint);
	}
	
	@Override
	protected void readParameters(){
		super.readParameters(); 
		FlatlandParameters fParameters = (FlatlandParameters) parameters; 
		fParameters.setFoodDist(Double.parseDouble(foodDist.getText()));
		fParameters.setPoisonDist(Double.parseDouble(poisonDist.getText()));
		if (dynamicButton.isSelected()) fParameters.setDynamicMap(true);
		else fParameters.setDynamicMap(false);
		fParameters.setNetworkLayout(setLayers());
	}
	
	@Override
	public void showDefaultParameters(){
		super.showDefaultParameters(); 
		FlatlandParameters fParameters = (FlatlandParameters) parameters; 
		poisonDist.setText(Double.toString(fParameters.getPoisonDist()));
		foodDist.setText(Double.toString(fParameters.getFoodDist()));
		if (fParameters.isDynamicMap()) this.dynamicButton.setSelected(true);
		else staticButton.setSelected(true);
		layers.setText(getHiddenLayers());
	}
	
	private String getHiddenLayers() {
		String s = "";
		int[] layout = ((FlatlandParameters) parameters).getNetworkLayout();
		
		if (layout.length == 2)
			return "";
		
		for (int i = 1; i < layout.length-1; i++) {
			s += Integer.toString(layout[i])+ ",";
		}
		s = s.substring(0, s.length()-1);
		return s;
	}
	
	private int[] setLayers() {
		String input = layers.getText().replaceAll("\\s", "");
		String returnString = "";
		
		if (input.length() == 0)
			returnString = "6,3";
		else returnString = "6," + input + "," + "3";

		String[] layersString = returnString.split(",");
		int[] layersInt = new int[layersString.length];
		for (int i = 0; i < layersString.length; i++) {
			if (! layersString[i].equals("") )
					layersInt[i] = Integer.parseInt(layersString[i]);
		}
//		System.out.println("Setlayers:" + Arrays.toString(layersInt));
		return layersInt;
	}
	
	
	
}
